package com.thyrsus.project.dao.event;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.thyrsus.project.dao.EMFService;
import com.thyrsus.project.dao.IDao;
import com.thyrsus.project.dao.event.entity.Event;


@Repository
public class EventDAO extends IDao<Event>{

	@SuppressWarnings("unchecked")
	public List<Event> listEvents() {
		EntityManager em = EMFService.get().createEntityManager();
		// Read the existing entries
		Query q = em.createQuery("select m from Event m");
		List<Event> Events = q.getResultList();
		return Events;
	}

	public Event getEvent(Long id) {
		EntityManager em = EMFService.get().createEntityManager();
		Event event = null;
		try {
			event = em.find(Event.class, id);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			em.close();
		}
		
		
		return event;
	}
	
	public Event remove(long id) {
		EntityManager em = EMFService.get().createEntityManager();
		Event event = null;
		try {
			event = em.find(Event.class, id);
			em.remove(event);
			
		} finally {
			em.close();
		}
		return event;
	}
	
	public void removeAll() {
		EntityManager em = EMFService.get().createEntityManager();
		Query q = em.createQuery("delete from Event");
		q.executeUpdate();
	}
}